import { createRouter } from 'vue-router'
import { useDetailStore } from '@/store/detail'
import { createWebHashHistory } from 'vue-router'
import printPage from '@/components/printPage.vue'
import { usePrintStore } from '@/store/print'
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'index',
      component: () => import('@/views/selectPage.vue'),
    }, {
      path: '/select',
      name: 'select',
      component: () => import('@/views/selectPage.vue')
    }, {
      path: '/multipleUpload',
      name: 'multipleUpload',
      component: () => import('@/views/multipleUploadPage.vue')
    }, {
      path: '/log',
      name: 'log',
      component: () => import('@/views/logPage.vue')
    },  {
      path: '/documentManagement',
      name: 'documentManagement',
      component: () => import('@/views/documentManagementPage.vue')
    }, {
      path: '/add',
      name: 'add',
      component: () => import('@/views/addPage.vue')
    }, {
      path: '/detail',
      name: 'detail',
      component: () => import('@/views/addPage.vue')
    }, {
      path: '/print',
      name: 'print',
      component: printPage
    }, {
      path: '/password',
      name: 'password',
      component: () => import('@/views/password.vue')
    }
  ],
})
router.beforeEach((to, from, next) => {
  // 进入详情页时,如果从add页面进入,则允许进入
  if (to.name === 'detail' && from.name === 'add') {
    next()
  } else
    // 进入详情页时,如果detailStore中没有数据,则跳转到选择页面
    if (to.name === 'detail' && !useDetailStore().detailId) {
      next({ name: 'select' })
    } else
      if (from.name === 'detail' && to.name !== 'detail'&&to.name!=="print") {
        // 切换到其他页面时清空detailStore
        useDetailStore().reset()
        console.log('离开详情页，清空detailStore');
        next()
      } else
        if (to.name === 'detail') {// 进入详情页时，将学生id存入detailStore
          console.log('进入详情页,学生id:', useDetailStore().detailId);
          next()

        } else
          // 切换到其他页面时清空printStore
          if (from.name === 'print' && to.name !== 'print') {
            usePrintStore().resetPrintData()
            next()

          } else
            // 进入打印页面时,如果printStore中没有数据,则跳转到选择页面
            if (to.name === 'print' && !usePrintStore().printData) {
              console.log("没有信息")
              next({ name: 'select' })
            } else {
              next()
            }
})
export default router
